export default {
  addUnreadAtListApp: (state, { pid, aid, nUnread }) => {
    let p = state.listApp.find((v) => { return v.id == pid })
    let s = p.list.find((v) => { return v.id == aid })
    p.unread += nUnread;
    s.unread += nUnread;
  },
  subUnreadAtListApp: (state, { pid, aid, nUnread }) => {
    let p = state.listApp.find((v) => { return v.id == pid })
    let s = p.list.find((v) => { return v.id == aid })
    p.unread -= nUnread;
    s.unread -= nUnread;
  },
  addUnreadAtListCus: (state, { msg }) => {
    // if (state.conditional.isOnlyNoReply && !msg.isFromCustomer) {
    //   return
    // }
    //按我理解，如果是“只看未回复”下，不应处理来自客服的msg，
    //并且需要移除现有，同列表客户
    //不过目前先不做过多不处理
    let idx = state.cusList_Page_state.list.findIndex((v, i) => {
      return v.id == msg.userId
    })
    if (idx < 0) {//不在当前列表中,插入第一
      state.cusList_Page_state.list.unshift({
        checked: false,
        eSCompanyId: msg.esCompanyId || msg.msg.esCompanyId,
        face: msg.avatar,
        id: msg.userId || msg.userList[0].userId,
        isStar: msg.isStar,
        lastMsg: msg.msg.msgtype == "text" ? msg.msg.text.content : '',
        lastTime: msg.createTimeDate,
        selfTag: msg.selfTag.length > 0 ? msg.selfTag : [{
          type: 1,
          typeName: "type1",
          isMultiple: false,
          selfTags: []
        }],//客户进度标签，客服修改单选
        nickname: msg.userName,
        tel: msg.mobile,
        type: msg.msg.msgtype,
        unread: msg.isFromCustomer ? 1 : 0
      })
    } else {//在当前列表中,移为第一
      let t = state.cusList_Page_state.list.splice(idx, 1)[0];
      state.cusList_Page_state.list.unshift(t);
      state.cusList_Page_state.list[0].type = msg.msg.msgtype;
      state.cusList_Page_state.list[0].lastMsg = msg.msg.msgtype == "text" ? msg.msg.text.content : '';
      state.cusList_Page_state.list[0].lastTime = msg.createTimeDate;
      if (msg.isFromCustomer) {
        state.cusList_Page_state.list[0].unread++;
      }
    };
  },
  subUnreadAtListCus: (state, { uid }) => {
    let cus = state.cusList_Page_state.list.find((c) => {
      return (c.id == uid || c.userId == uid)
    })//不一定能找到
    if (cus) { cus.unread = 0; }
  },


  setMsgList: (state, { arr }) => {
    state.msgList.splice(0, state.msgList.length);
    arr.forEach((v, i, a) => {
      state.msgList.push(v)
    });
  },
  addMsgList: (state, newMsg) => {
    state.msgList.splice(0, 0, newMsg)
  },
  updataWsMsgIn: (state, { from, msg }) => {
    switch (from) {
      case 'cusWS':
        state.wsMsgFromCus = msg;
        break;
      case 'appWs':
        state.wsMsgFromApp = msg;
        break;
      default:
        console.error("收到来源不明的消息", from, msg)
    }
    // state.wsMsgIn = value
  },
  readySendMsgFromApp: (state, { messageObj }) => {
    console.log('readySendMsg', messageObj)
    state.wsMsgToApp = messageObj;
  },
};